package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.BaseResponseListener;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.JsonBean;
import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.Parameter;
import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.model.UserModel;
import com.ljkj.blockpay.network.ApiToCConfig;
import com.ljkj.blockpay.network.MainRepository;
import com.ljkj.blockpay.network.response.DisposableListCallBack;
import com.ljkj.blockpay.presenter.inter.RechargeInter;
import com.ljkj.blockpay.widget.LoadingDialog;
import com.luck.picture.lib.tools.ToastUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarInputStream;

public class RechargePresenter extends BasePresenterImpl<RechargeInter.RechargeInterView> implements RechargeInter.RechargeInterPresenter {



    public String preOrderId;
    public String networkType;
//    private LoadingDialog loadingDialog;
//    public void showLoading() {
//        if (loadingDialog == null) {
//            loadingDialog = new LoadingDialog.Builder(getContext()).create();
//        }
//        loadingDialog.show();
//    }
//
//    public void dismisLoading() {
//        if (loadingDialog != null) {
//            loadingDialog.dismiss();
//        }
//    }

    public RechargePresenter(@NonNull Context context, @NonNull RechargeInter.RechargeInterView view) {
        super(context, view);
    }


//业务场景
    @Override
    public void getNetAccuracy(Integer orderType) {
//        showLoading();
        HttpRequest.GET(getContext(), ApiToCConfig.URL_TRADE_NETWORK_ACCURACY, new Parameter().add("orderType", orderType), new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
//                    dismisLoading();
                   JsonList jsonList = main.getList("data");
                   List<RechargeModel> list = new ArrayList<>();
                    for (int i = 0; i < jsonList.size(); i++) {
                        list.add(JsonBean.getBean(jsonList.getJsonMap(i), RechargeModel.class));
                    }
                    getView().showNetAccuracy(list);
                    Log.d("zdl","==onSafeSuccess==="+list.size());
                }else{
//                    dismisLoading();
                }
            }
        });
//        addDisposable(MainRepository.getInstance().getNetAccuracy()
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeWith(new DisposableListCallBack<UserModel>(){
//            @Override
//            protected void onSafeSuccess(List<UserModel> list) {
//                super.onSafeSuccess(list);
//                Log.d("zdl","==onSafeSuccess==="+list.size());
//
//            }
//
//            @Override
//            protected void onSafeFailed(String code, String message) {
//                Log.d("zdl","===onSafeFailed=="+message);
//            }
//        }));
    }

    //二维码充值地址
    @Override
    public void getRecharge(String currencyType, String networkType) {
//        showLoading();
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("currencyType",currencyType);
        jsonMap.put("networkType",networkType);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_RECHARGE, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
//                    dismisLoading();
                    JsonMap jsonMapdata = main.getJsonMap("data");
                    getView().showrecharge(jsonMapdata);

                } else {
//                    dismisLoading();
                }


            }
        });
    }

    //订单预创建
    @Override
    public void preConfirm(String bizToken,Number originAmount, Integer orderType, String networkType,String targetAddress,String originCurrency,String targetCurrency,String remark) {
        showLoading();
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("bizToken",bizToken);
        jsonMap.put("orderType",orderType);
        jsonMap.put("networkType",networkType);
        jsonMap.put("originAddress","");
        jsonMap.put("originAmount",originAmount);
        jsonMap.put("originCurrency",originCurrency);
        jsonMap.put("remark",remark);
        jsonMap.put("targetAddress",targetAddress);
        jsonMap.put("targetCurrency",targetCurrency);
        jsonMap.put("targetUser","");
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_ORDER_PRECONFIRM, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    dismisLoading();
                 JsonMap jsonMapData =   main.getJsonMap("data");
                 getView().getOrderDetails(jsonMapData);
                } else {
                    dismisLoading();
                }
            }
        });
    }
    //创建订单
    @Override
    public void createOrder(Integer orderType,String preOrderId, String tradePwd) {
        showLoading();
        if (preOrderId == null){
            ToastUtils.s(getContext(),"未获取到订单id");
            return;
        }
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("orderType",orderType);
        jsonMap.put("preOrderId",preOrderId);
        jsonMap.put("tradePwd",tradePwd);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_ORDER_CREATE, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    dismisLoading();
                    JsonMap jsonMapData =   main.getJsonMap("data");
                    getView().getOrderDetails2(jsonMapData);
                } else {
                    dismisLoading();
                }
            }
        });
    }

//查询币种余额
    @Override
    public void SearchCurrencyBalance(String currencyType, Integer orderType) {
        showLoading();
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("currencyType",currencyType);
        jsonMap.put("orderType",orderType);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_SEARCH_CURRENCY, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    dismisLoading();
                    JsonMap jsondata = main.getJsonMap("data");
                    getView().SearchCurrencySuccess(jsondata);

                } else {
                    dismisLoading();
                }
            }
        });
    }


}
